<template>
  <div class="main-layout">
    <!-- 侧边栏 -->
    <el-aside v-show="!isCollapsed" :width="sidebarWidth" class="sidebar">
      <!-- Logo区域 -->
      <div class="logo">
        <div class="logo-container">
          <img :src="logo" alt="唐山钢厂管理系统" class="logo-image" />
        </div>
      </div>
      
      <!-- 菜单区域 -->
      <el-scrollbar class="menu-scrollbar">
        <el-menu
          :default-active="activeMenu"
          :collapse="isCollapsed"
          :unique-opened="true"
          class="sidebar-menu"
          router
        >
          <template v-for="menu in visibleMenus" :key="menu.id">
            <!-- 有子菜单的情况 -->
            <el-sub-menu v-if="menu.children && menu.children.length > 0" :index="menu.path" class="menu-item-group">
              <template #title>
                <div class="menu-item-content">
                  <el-icon class="menu-icon"><component :is="menu.icon" /></el-icon>
                  <span class="menu-text">{{ menu.name }}</span>
                  <div class="menu-indicator"></div>
                </div>
              </template>
              <el-menu-item
                v-for="child in menu.children"
                :key="child.id"
                :index="child.path"
                class="menu-sub-item"
              >
                <div class="menu-item-content">
                  <el-icon class="menu-icon"><component :is="child.icon" /></el-icon>
                  <span class="menu-text">{{ child.name }}</span>
                  <div class="menu-indicator"></div>
                </div>
              </el-menu-item>
            </el-sub-menu>
            
            <!-- 无子菜单的情况 -->
            <el-menu-item v-else :index="menu.path" class="menu-item">
              <div class="menu-item-content">
                <el-icon class="menu-icon"><component :is="menu.icon" /></el-icon>
                <span class="menu-text">{{ menu.name }}</span>
                <div class="menu-indicator"></div>
              </div>
            </el-menu-item>
          </template>
        </el-menu>
      </el-scrollbar>
      
      <!-- 侧边栏底部装饰 -->
      <div class="sidebar-footer" v-show="!isCollapsed">
        <div class="footer-info">
          <div class="version-info">v1.0.0</div>
          <div class="copyright">© 2025 新华冶金智慧安防平台</div>
        </div>
      </div>
    </el-aside>

    <!-- 主内容区 -->
    <el-container class="main-container">
      <!-- 顶部导航栏 -->
      <el-header class="header">
        <div class="header-left">
          <el-button
            :icon="isCollapsed ? Expand : Fold"
            @click="toggleSidebar"
            text
            class="collapse-btn"
          />
          
          <!-- 面包屑导航 -->
          <el-breadcrumb class="breadcrumb" separator=">">
            <el-breadcrumb-item
              v-for="item in breadcrumb"
              :key="item.path"
              :to="item.path"
            >
              {{ item.name }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="header-right">
          <!-- 全屏切换 -->
          <el-tooltip content="全屏" placement="bottom">
            <el-button :icon="FullScreen" @click="toggleFullscreen" text />
          </el-tooltip>
          
          <!-- 用户信息 -->
          <el-dropdown @command="handleUserCommand">
            <div class="user-info">
              <el-avatar :size="32" :src="user?.avatar" class="user-avatar">
                <el-icon><User /></el-icon>
              </el-avatar>
              <span class="user-name">{{ userName }}</span>
              <el-icon class="user-arrow"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="changePassword">
                  <el-icon><Lock /></el-icon>
                  修改密码
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主内容 -->
      <el-main class="main-content">

        <!-- 具体内容区域 -->
        <div class="page-content-section">
          <router-view v-slot="{ Component }">
            <transition name="fade" mode="out-in">
              <component :is="Component" />
            </transition>
          </router-view>
        </div>
      </el-main>
    </el-container>
    
    <!-- 修改密码对话框 -->
    <ResetPasswordDialog
      :visible="showChangePasswordDialog"
      :userData="user"
      @update:visible="showChangePasswordDialog = $event"
      @success="handlePasswordChangeSuccess"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import {
  ElAside,
  ElContainer,
  ElHeader,
  ElMain,
  ElMenu,
  ElMenuItem,
  ElSubMenu,
  ElButton,
  ElBreadcrumb,
  ElBreadcrumbItem,
  ElDropdown,
  ElDropdownMenu,
  ElDropdownItem,
  ElAvatar,
  ElIcon,
  ElTooltip,
  ElScrollbar
} from 'element-plus'
import {
  Fold,
  Expand,
  FullScreen,
  User,
  ArrowDown,
  SwitchButton,
  Lock
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'
import { useMenuStore } from '@/stores/menu'
import { confirm } from '@/utils/confirm'
import ResetPasswordDialog from '@/views/system/user/components/ResetPasswordDialog.vue'

// Store
const authStore = useAuthStore()
const menuStore = useMenuStore()
const route = useRoute()

// 响应式状态
const isCollapsed = ref(false)
const isFullscreen = ref(false)
const showChangePasswordDialog = ref(false)

// 计算属性
const user = computed(() => authStore.user)
const userName = computed(() => authStore.userName)
const visibleMenus = computed(() => menuStore.visibleMenus)
const activeMenu = computed(() => route.path)
const breadcrumb = computed(() => menuStore.getBreadcrumb(route.path))
const sidebarWidth = computed(() => '240px')


import logo from '@/assets/home_logo.jpg'
// 方法
const toggleSidebar = () => {
  isCollapsed.value = !isCollapsed.value
  menuStore.toggleCollapsed()
}

const toggleFullscreen = () => {
  if (!isFullscreen.value) {
    document.documentElement.requestFullscreen()
  } else {
    document.exitFullscreen()
  }
}

const handleUserCommand = (command: string) => {
  switch (command) {
    case 'changePassword':
      // 显示修改密码对话框
      showChangePasswordDialog.value = true
      break
    case 'settings':
      // 跳转到设置
      console.log('Go to settings')
      break
    case 'logout':
      handleLogout()
      break
  }
}

const handleLogout = () => {
  confirm.exit('确定要退出登录吗？').then(() => {
    authStore.logout()
  }).catch(() => {
    // 用户取消退出
  })
}

// 处理修改密码成功
const handlePasswordChangeSuccess = () => {
  showChangePasswordDialog.value = false
}

// 监听全屏状态变化
const onFullscreenChange = () => {
  isFullscreen.value = !!document.fullscreenElement
}

// 监听路由变化，更新激活菜单
watch(
  () => route.path,
  (newPath) => {
    menuStore.setActiveMenu(newPath)
  },
  { immediate: true }
)

// 初始化
onMounted(async () => {
  // 初始化菜单
  await menuStore.initMenus()
  
  // 监听全屏事件
  document.addEventListener('fullscreenchange', onFullscreenChange)
})

</script>

<style scoped lang="scss">
.main-layout {
  height: 100vh;
  display: flex;
}

.sidebar {
  background: #ffffff;
  transition: all 0.3s ease;
  overflow: hidden;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.08);
  position: relative;
  z-index: 100;
  border-right: 1px solid #f0f0f0;
  width: 220px;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  border-bottom: 1px solid #f0f0f0;
  position: relative;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  width: 100%;
}

.logo-image {
  max-width: 180px;
  max-height: 48px;
  width: auto;
  height: auto;
  object-fit: contain;
}

.menu-scrollbar {
  height: calc(100vh - 125px);
  padding: 12px 0;
}

.sidebar-menu {
  border: none;
  background: transparent;
  padding: 0;
}

.menu-item,
.menu-item-group,
.menu-sub-item {
  margin: 4px 16px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
}

.menu-item-content {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 8px;
  position: relative;
  z-index: 1;
}

.menu-icon {
  font-size: 16px;
  width: 18px;
  display: flex;
  justify-content: center;
  transition: all 0.3s ease;
}

.menu-text {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.menu-indicator {
  width: 0;
  height: 100%;
  position: absolute;
  right: 0;
  top: 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 0 6px 6px 0;
  transition: width 0.3s ease;
}

.sidebar-menu :deep(.el-menu-item),
.sidebar-menu :deep(.el-sub-menu__title) {
  color: #4a5568;
  border: none;
  height: 44px;
  line-height: 44px;
  background: transparent;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  margin-bottom: 2px;
}

.sidebar-menu :deep(.el-menu-item:hover),
.sidebar-menu :deep(.el-sub-menu__title:hover) {
  color: #667eea;
  background: rgba(102, 126, 234, 0.08);
  transform: translateX(2px);
}

.sidebar-menu :deep(.el-menu-item:hover .menu-indicator) {
  width: 3px;
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  color: #667eea;
  border-left: 3px solid #667eea;
  padding-left: 13px;
}

.sidebar-menu :deep(.el-menu-item.is-active .menu-icon) {
  color: #667eea;
}

.sidebar-menu :deep(.el-sub-menu .el-menu-item) {
  margin: 2px 8px;
  height: 40px;
  line-height: 40px;
  padding-left: 36px !important;
  background: transparent;
  border-radius: 6px;
  border-left: none;
}

.sidebar-menu :deep(.el-sub-menu .el-menu-item:hover) {
  background: rgba(102, 126, 234, 0.06);
  transform: translateX(1px);
}

.sidebar-menu :deep(.el-sub-menu .el-menu-item.is-active) {
  background: rgba(102, 126, 234, 0.12);
  color: #667eea;
  border-left: 2px solid #667eea;
  padding-left: 34px !important;
}

.sidebar-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  background: #ffffff;
  border-top: 1px solid #f0f0f0;
}

.footer-info {
  text-align: center;
  color: #a0aec0;
}

.version-info {
  font-size: 11px;
  font-weight: 500;
  margin-bottom: 4px;
  color: #667eea;
}

.copyright {
  font-size: 10px;
  color: #cbd5e0;
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #f7fafc;
  width: 100%;
  transition: all 0.3s ease;
}

.header {
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 60px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.collapse-btn {
  font-size: 16px;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  transition: all 0.3s ease;
  background: #f7fafc;
  color: #4a5568;
  border: 1px solid #e2e8f0;
}

.collapse-btn:hover {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

.breadcrumb {
  font-size: 14px;
  font-weight: 500;
}

.breadcrumb :deep(.el-breadcrumb__item) {
  color: #718096;
}

.breadcrumb :deep(.el-breadcrumb__item:last-child) {
  color: #667eea;
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f7fafc;
  border: 1px solid #e2e8f0;
}

.user-info:hover {
  background: rgba(102, 126, 234, 0.08);
  border-color: #667eea;
}

.user-avatar {
  border: 2px solid #e2e8f0;
}

.user-name {
  font-size: 14px;
  color: #2d3748;
  font-weight: 500;
}

.user-arrow {
  font-size: 12px;
  color: #a0aec0;
  transition: transform 0.3s ease;
}

.user-info:hover .user-arrow {
  transform: rotate(180deg);
  color: #667eea;
}

.main-content {
  background: #f7fafc;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
}


/* 页面内容区域 */
.page-content-section {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background: transparent;
}

/* 页面切换动画 */
.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  transform: translateY(8px);
}

/* 滚动条美化 */
.menu-scrollbar :deep(.el-scrollbar__bar) {
  opacity: 0.2;
}

.menu-scrollbar :deep(.el-scrollbar__thumb) {
  background: rgba(102, 126, 234, 0.2);
  border-radius: 4px;
}

.menu-scrollbar :deep(.el-scrollbar__thumb:hover) {
  background: rgba(102, 126, 234, 0.4);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    z-index: 999;
    transform: translateX(-100%);
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
    width: 240px !important;
  }
  
  .sidebar.mobile-show {
    transform: translateX(0);
  }
  
  .main-container {
    margin-left: 0;
    width: 100%;
  }
  
  .header-left {
    gap: 12px;
  }
  
  .breadcrumb {
    display: none;
  }
}

/* 子菜单箭头美化 */
.sidebar-menu :deep(.el-sub-menu__icon-arrow) {
  color: #a0aec0;
  transition: all 0.3s ease;
}

.sidebar-menu :deep(.el-sub-menu.is-opened .el-sub-menu__icon-arrow) {
  color: #667eea;
}

/* 菜单项图标美化 */
.sidebar-menu :deep(.el-icon) {
  transition: all 0.3s ease;
}

.sidebar-menu :deep(.el-menu-item:hover .el-icon),
.sidebar-menu :deep(.el-sub-menu__title:hover .el-icon) {
  transform: scale(1.1);
}

.sidebar-menu :deep(.el-menu-item.is-active .el-icon) {
  transform: scale(1.1);
}
</style>